import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

import cssi_data

class Student(object):
  def __init__(self,name,full_name,photo=None,buddy=None):
    self.name = name
    self.full_name = full_name
    self.photo = photo
    self.buddy=buddy
class HomeHandler(webapp.RequestHandler):
  def get(self):
    students = []
    for name in cssi_data.STUDENTS:
      student = Student(name, cssi_data.FULL_NAMES[name])
      students.append(student)


    template_values = {'students': students}
    
    path = os.path.join(os.path.dirname(__file__), 'home.html')
    self.response.out.write(template.render(path, template_values))

class ProfileHandler(webapp.RequestHandler):
  def get(self):
    name= self.request.get('name')
    if name in cssi_data.STUDENTS:
      full_names = cssi_data.FULL_NAMES[name]
      buddy = cssi_data.BUDDIES.get(name)
      photo = cssi_data.PHOTOS.get(name)
      template_file = 'profile.html'
    else:
      self.error(404)
      template_file = 'profile_error.html'
    template_values = {'student' : Student(name, full_names,photo,buddy)}
    path = os.path.join(os.path.dirname(__file__), template_file)
    self.response.out.write(template.render(path, template_values))

def main():
  application = webapp.WSGIApplication([('/', HomeHandler),
                                        ('/profile.*', ProfileHandler)],
                                       debug=True)
  run_wsgi_app(application)

if __name__ == '__main__':
  main()
